<template>
  <div class="search-history">
    <!-- 搜索历史按钮 -->
    <van-cell title="搜索历史">
      <!-- 删除状态 -->
      <div v-if="isDeleteShow">
        <span @click="$emit('clear-search-history')">全部删除</span>&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <!-- 非删除状态 -->
      <van-icon name="delete" v-else @click="isDeleteShow = true"></van-icon>
    </van-cell>

    <!-- 搜索历史项 -->
    <van-cell
      :title="item"
      v-for="(item, index) in searchHistorys"
      :key="index"
      @click="onSearchItemClick(item, index)"
    >
      <van-icon v-show="isDeleteShow" name="close"></van-icon>
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchHistorys: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      isDeleteShow: false
    }
  },
  methods: {
    // 搜索历史项点击触发
    onSearchItemClick(item, index) {
      if (this.isDeleteShow) {
        // 删除状态,删除历史记录项
        this.searchHistorys.splice(index, 1)
      } else {
        // 非删除状态,进入搜索内容
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>